package com.stjb.furns.dao.impl;

import com.stjb.furns.dao.BasicDAO;
import com.stjb.furns.dao.FurnDAO;
import com.stjb.furns.entity.Furn;

import java.util.List;

/**
 * @author situ
 * @version 1.0
 */
public class FurnDAOImpl extends BasicDAO implements FurnDAO {

    /**
     * 查询家具信息
     *
     * @return
     */
    @Override
    public List<Furn> queryFurns() {
        String sql = "SELECT `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` as imgPath \n" +
                "FROM furn";
        return queryMulti(sql, Furn.class);

    }


    /**
     * 添加家具
     *
     * @param furn
     * @return
     */
    @Override
    public int addFurn(Furn furn) {

        String sql = "INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) \n" +
                "VALUES(NULL , ? , ? , ? , ? , ? , ?)";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());
    }

    /**
     * 删除家具
     *
     * @param id
     * @return 返回1删除成功(返回受影响行数)
     */
    @Override
    public int delete(int id) {
        String sql = "DELETE FROM furn WHERE `id`=?";

        return update(sql, id);
    }

    @Override
    public Furn queryFurnById(int id) {
        String sql = "SELECT `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` as imgPath \n" +
                "FROM furn where `id`=?";
        return (Furn) querySingle(sql, Furn.class, id);
    }

    /**
     * 根据id将传入的数据更新到数据库
     *
     * @param furn
     * @return 返回1
     */
    @Override
    public int updateFurn(Furn furn) {
        String sql = "UPDATE furn SET `name`=?, `maker`=?, `price`=?, `sales`=?, `stock`=?, \n" +
                "`img_path`=? WHERE `id`=?";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(),
                furn.getStock(), furn.getImgPath(), furn.getId());
    }

    @Override
    public int getTotalRow() {
        String sql = "SELECT COUNT(*) FROM `furn`";
        return ((Number) queryScalar(sql)).intValue();
    }

    /**
     * sql分页查询语句
     *
     * @return
     */
    @Override
    public List<Furn> getPageItems(int begin, int pageSize) {
        String sql = "SELECT `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` as imgPath \n" +
                "FROM furn limit ?,?";
        return queryMulti(sql, Furn.class, begin, pageSize);
    }

    /**
     * 根据名字模糊查询
     *
     * @param name
     * @return
     */
    @Override
    public int getTotalRowByName(String name) {

        String sql = "SELECT count(*) FROM furn where `name` like ?";
        return ((Number) queryScalar(sql, "%" + name + "%")).intValue();

    }

    @Override
    public List<Furn> grtPageItemByName(int begin, int pageSize, String name) {
        String sql = "SELECT `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` as imgPath \n" +
                "FROM furn where `name` like ? limit ?,?";
        return queryMulti(sql, Furn.class, "%" + name + "%", begin, pageSize);
    }
}
